/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.jpatch.access;

import java.util.ArrayList;
import java.util.List;

import milk.jpatch.CPoolMap;
import milk.jpatch.Patch;
import milk.jpatch.access.AccessFlagsPatch.AccessLevel;

/**
 * Denotes a patch in the remaining (non access right) flags.
 * @author Thomas Kerber
 * @version 1.0.0
 */
public class RestFlagsPatch implements Patch<Integer>{
    
    private static final long serialVersionUID = -5228718408448773709L;
    
    /**
     * The flag patches to apply.
     */
    private final FlagPatch[] flagPatches;
    
    /**
     * Creates a new patch.
     * @param flagPatches The flags patches to delegate to.
     */
    public RestFlagsPatch(FlagPatch[] flagPatches){
        this.flagPatches = flagPatches;
    }
    
    /**
     * Generates a new patch.
     * @param old The old flags.
     * @param new_ The new flags.
     * @param a The access level of the flags.
     * @return The patch to get from the old flags to the new.
     */
    public static RestFlagsPatch generate(int old, int new_, AccessLevel a){
        int irrelevantDigits = a == AccessLevel.CLASS ? 1 : 3;
        // 2^x - 1 turns the last x bits on.
        int mask = ~((1 << irrelevantDigits) - 1);
        
        int oldRelevant = old & mask;
        int newRelevant = new_ & mask;
        
        List<FlagPatch> flagPatches = new ArrayList<FlagPatch>();
        // Remember: Flags have at most 16 bits.
        for(int i = 0; i < 16; i++){
            mask = 1 << i;
            int oldAt = (oldRelevant & mask) >> i;
            int newAt = (newRelevant & mask) >> i;
            if(oldAt != newAt)
                flagPatches.add(new FlagPatch(i, newAt == 1));
        }
        return new RestFlagsPatch(
                flagPatches.toArray(new FlagPatch[flagPatches.size()]));
    }
    
    /**
     * 
     * @return The contained flag patches.
     */
    public FlagPatch[] getFlagPatches(){
        return flagPatches;
    }
    
    /**
     * Patches the flags.
     * @param flags The flags to patch.
     * @return The patched flags.
     */
    public int patch(int flags){
        for(FlagPatch f : flagPatches){
            flags = f.patch(flags);
        }
        return flags;
    }
    
    @Override
    public final Integer patch(Integer flags, CPoolMap map){
        if(flags == null)
            return null;
        return patch(flags.intValue());
    }
    
}
